﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XsClient.Common;

namespace XsClient.Controls
{
    /// <summary>
    /// UCBookContext.xaml 的交互逻辑
    /// </summary>
    public partial class UCBookContext : UserControl
    {
        /// <summary>
        /// 下一章
        /// </summary>
        public event EventHandler Next;
        /// <summary>
        /// 上一章
        /// </summary>
        public event EventHandler Prev;

        public UCBookContext()
        {
            InitializeComponent();
            Init();
        }
        List<FontFamily> fonts = null;
        private void Init()
        {
            try
            {
                fonts = new List<FontFamily>();
                XsSysConfig sysConfig = XsSysConfig.Current;
                if (sysConfig != null && sysConfig.FontSize > 10)
                {
                    this.txtContext.FontSize = sysConfig.FontSize;
                }
                int selIndex = -1;

                foreach (var item in Fonts.SystemFontFamilies)
                {
                    if (item.FamilyNames.ContainsKey(System.Windows.Markup.XmlLanguage.GetLanguage("zh-cn")))
                    {
                        fonts.Add(item);
                        if (sysConfig != null && !string.IsNullOrEmpty(sysConfig.FontName))
                        {
                            if (item.Source == sysConfig.FontName)
                            {
                                selIndex = this.cmbFont.Items.Count;
                            }
                        }
                        this.cmbFont.Items.Add(item.FamilyNames[System.Windows.Markup.XmlLanguage.GetLanguage("zh-cn")]);
                    }
                    //else
                    //    this.cmbFont.Items.Add(item.Source);
                }
                if (selIndex >= 0)
                    cmbFont.SelectedIndex = selIndex;
                cpBackground.IsMeasuredValueVisible = true;
                cpBackground.IsDefaultColorPanelVisible = false;
                cpBackground.IsOpacityEnabled = false;
                if (!string.IsNullOrEmpty(sysConfig.Background))
                    cpBackground.SelectedBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(sysConfig.Background));
                else
                    cpBackground.SelectedBrush = new SolidColorBrush(Color.FromArgb(255, 29, 29, 29));
                cpForeground.IsMeasuredValueVisible = true;
                cpForeground.IsDefaultColorPanelVisible = false;
                cpForeground.IsOpacityEnabled = false;
                if (!string.IsNullOrEmpty(sysConfig.Forecolor))
                    cpForeground.SelectedBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(sysConfig.Forecolor));
                else
                    cpForeground.SelectedBrush = new SolidColorBrush(Color.FromArgb(255, 220, 220, 220));
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
            // this.cmbFont.ItemsSource = fonts;
        }

        public void ShowData(string data)
        {
            txtContext.Dispatcher.BeginInvoke(new Action(() =>
            {
                txtContext.Document.Blocks.Clear();
                if (!string.IsNullOrEmpty(data))
                    txtContext.AppendText(data.Replace("<br>", "\r\n").Replace("\r\n\r\n", "\r\n") + "\r\n");
                txtContext.ScrollToHome();
            }));
        }

        private void CmbFontSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (txtContext != null)
            {
                txtContext.Dispatcher.BeginInvoke(new Action(() =>
                {
                    txtContext.FontSize = int.Parse(((ComboBoxItem)cmbFontSize.SelectedItem).Content.ToString());
                    XsSysConfig sysConfig = XsSysConfig.Current;
                    sysConfig.FontSize = (int)txtContext.FontSize;
                    sysConfig.Save();
                }));
            }
        }

        private void CmbFont_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (cmbFont.SelectedIndex >= 0)
                {
                    var font = fonts[cmbFont.SelectedIndex];
                    txtContext.FontFamily = font;
                    XsSysConfig sysConfig = XsSysConfig.Current;
                    sysConfig.FontName = font.Source;
                    sysConfig.Save();
                }
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }

        private void CpBackground_SelectedBrushChanged(object sender, Panuon.UI.Silver.Core.SelectedBrushChangedEventArgs e)
        {
            txtContext.Background = e.SelectedBrush;
            XsSysConfig sysConfig = XsSysConfig.Current;
            sysConfig.Background = string.Format("{0}", txtContext.Background.ToString());
            sysConfig.Save();
        }

        private void CpForeground_SelectedBrushChanged(object sender, Panuon.UI.Silver.Core.SelectedBrushChangedEventArgs e)
        {
            txtContext.Foreground = e.SelectedBrush;
            XsSysConfig sysConfig = XsSysConfig.Current;
            sysConfig.Forecolor = string.Format("{0}", txtContext.Foreground.ToString());
            sysConfig.Save();
        }

        private void BtnPre_Click(object sender, RoutedEventArgs e)
        {
            if (Prev != null)
            {
                Prev(sender, e);
            }
        }

        private void BtnNext_Click(object sender, RoutedEventArgs e)
        {

            if (Next != null)
            {
                Next(sender, e);
            }
        }

        private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (gRow01.Height.Value > 20)
                gRow01.Height = new GridLength(1);
            else
                gRow01.Height = new GridLength(40);

        }
    }
}
